package model;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import controller.Controller;

import view.MainWindow;
import object.Point;



public class Model {
	public final int xSize;
	public final int ySize;
	Point[][] points;
	private JFrame frame;
	private boolean clickEvent = false;
	
	ArrayList<Point> route = new ArrayList<Point>();
		
	private Model() {
		Dimension bounds = Toolkit.getDefaultToolkit().getScreenSize();
		xSize = (int) bounds.getWidth();
		ySize = (int) bounds.getHeight();
		
		System.out.println("Model x:" + xSize + " y:" + ySize);
		
		points = new Point[xSize][ySize]; 
		for(int x = 0; x < xSize; x++){
			for(int y = 0; y < ySize; y++){
				points[x][y] = new Point(x,y);
			}
		}
		
		SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run(){
                frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setMinimumSize(new Dimension(xSize,ySize));
                frame.setUndecorated(true);
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                frame.setLocationRelativeTo(null);
                MainWindow main = new MainWindow();
                frame.getContentPane().add(main, BorderLayout.CENTER);
                frame.setFocusable(false);
                frame.setVisible(true);
                main.setFocusable(true);
                main.requestFocus();
                main.requestFocusInWindow();
                main.addKeyListener(new KeyListener() {

                    @Override
                    public void keyTyped(KeyEvent e) {}

                    @Override
                    public void keyReleased(KeyEvent e) {}

                    @Override
                    public void keyPressed(KeyEvent e) {
                    	char pressed = e.getKeyChar();
                    	pressed = Character.toLowerCase(pressed);
                    	int code = e.getKeyCode();
                    	System.out.println("key press: " + pressed + " num: " + code);
                        if(e.getKeyChar()=='q' || pressed == KeyEvent.VK_ESCAPE || code == 81){
                        	System.exit(0);
                        } else if(pressed == 'r'){
                        	Controller.generateRandom(points);
                        } else if(pressed == 'g'){
                        	Controller.generateMap(points);
                        } else if(pressed == 'c'){
                        	route.clear();
                        	Controller.clearPoints(points);
                        } else if(pressed == 'd'){
                        	route.clear();
                        	Controller.resetPoints(points);
                        } else if(pressed == 'a'){
                        	Controller.aStar(points, route);
                        }
                        
                    }
                });
                main.addMouseListener(new MouseListener() {
        			@Override
        			public void mouseClicked(MouseEvent event) {
        				if(clickEvent == true)
        					return;
        				clickEvent = true;
        				int x = event.getX();
        				int y = event.getY();
        				System.out.println("Map.mouseClicked x:" + x + " y:" + y);
        				points[x][y].setCurrentColor(Color.RED);
        				route.add(points[x][y]);
        				clickEvent = false;
        			}
        			@Override
        			public void mouseEntered(MouseEvent e) {}
        			@Override
        			public void mouseExited(MouseEvent e) {}
        			@Override
        			public void mousePressed(MouseEvent e) {}
        			@Override
        			public void mouseReleased(MouseEvent e) {}
        		});
            }
        });
	}
	
	private static Model instance = new Model();
	public static Model getInstance(){
		return instance;
	}
	public int getColor(int x, int y) {
		return points[x][y].getCurrentColor().getRGB();
	}
}
